iT邦幫忙

2024 iThome 鐵人賽

DAY 1
0
自我挑戰組

Angular 元件煉成練習計畫系列 第 1

元件:按鈕 - 顏色主題色控制

  • 分享至 

  • xImage
  •  

菜雞作者發現directive 沒處理好 bug 還沒修
/images/emoticon/emoticon04.gif

按鈕

以往慣性使用Angular Material Button
常常在使用時沒有考慮到太多情境
這裡希望可以整理合併一下開發過程遇到的問題
暫時脫離 Material


目標:顏色主題色控制

https://ithelp.ithome.com.tw/upload/images/20240915/20168166QAmxSnCnR3.png

https://ithelp.ithome.com.tw/upload/images/20240915/20168166NGVsqm3Igu.png
借用coloor 生成切換目標顏色
開發時常常遇到主色、次要顏色、警告色、.......

https://ithelp.ithome.com.tw/upload/images/20240915/20168166IUJHugvQsT.png

  1. 建立基本樣式
button {
  color: white;
  background-color: black;
  padding: 4px 8px;
  min-width: 36px;
  border-radius: 5px;
  height: 24px;
  line-height: 12px;
}
  1. scss變數 + 樣式 css
//第一系統
$s1-primary: #D62828;
$s1-secondary: #003049;
$s1-tertiary: #F77F00;
$s1-neutral: #EAE2B7;
$s1-neutral-variant: #FCBF49;

button.primary {
  background-color: $s1-primary;
}

button.secondary {
  background-color: $s1-secondary;
}

button.tertiary {
  background-color: $s1-tertiary;
}

button.neutral {
  background-color: $s1-neutral;
}

button.neutral-variant {
  background-color: $s1-neutral-variant;
}

//第二系統
$s2-primary: #606C38;
$s2-secondary: #283618;
$s2-tertiary: #FEFAE0;
$s2-neutral: #DDA15E;
$s2-neutral-variant: #BC6C25;

button.primary.s2 {
  background-color: $s2-primary;
}

button.secondary.s2 {
  background-color: $s2-secondary;
}

button.tertiary.s2 {
  background-color: $s2-tertiary;
}

button.neutral.s2 {
  background-color: $s2-neutral;
}

button.neutral-variant.s2 {
  background-color: $s2-neutral-variant;
}

噁心的東西
3. Directive 處理 ? 走歪一下

<div class="flex gap">
  <button appThemeColor="primary">$s1-primary</button>
  <button appThemeColor="secondary">$s1-secondary</button>
  <button appThemeColor="tertiary">$s1-tertiary</button>
  <button appThemeColor="neutral">$s1-neutral</button>
  <button appThemeColor="neutral-variant">$s1-neutral-variant</button>
</div>
import {
  AfterViewInit,
  Directive,
  effect,
  ElementRef,
  inject,
  input,
  Input,
  Renderer2,
  signal,
} from '@angular/core';

@Directive({
  selector: '[appThemeColor]',
  standalone: true,
})
export class ThemeColorDirective  {
  appThemeColor = input<string>('');

  elementRef = inject(ElementRef);
  private renderer = inject(Renderer2);
  constructor() {
    effect(() => {
      this.applyThemeColor(this.appThemeColor());
    });
  }

  private applyThemeColor(colorTheme: string | null) {
    if (colorTheme) {
      this.renderer.addClass(this.elementRef.nativeElement, colorTheme);
    }
  }
}

  1. 改寫class 處理方式 + 使用service
.s1-theme {
  button.primary {
    background-color: $s1-primary;
  }

  button.secondary {
    background-color: $s1-secondary;
  }

  button.tertiary {
    background-color: $s1-tertiary;
  }

  button.neutral {
    background-color: $s1-neutral;
  }

  button.neutral-variant {
    background-color: $s1-neutral-variant;
  }
}


.s2-theme {

  button.primary {
    background-color: $s2-primary;
  }

  button.secondary {
    background-color: $s2-secondary;
  }

  button.tertiary {
    background-color: $s2-tertiary;
  }

  button.neutral {
    background-color: $s2-neutral;
  }

  button.neutral-variant {
    background-color: $s2-neutral-variant;
  }
}

import { Injectable, signal } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ThemeControlService {
  private themeSignal = signal<string>('');

  constructor() { }

  setTheme(theme: string) {
    const currentTheme = this.themeSignal();
    if (currentTheme && currentTheme !== theme) {
      document.body.classList.remove(currentTheme);
    }
    if (theme) {
      document.body.classList.add(theme);
      this.themeSignal.set(theme);
    }
  }


  getTheme() {
    return this.themeSignal.asReadonly();
  }
}

https://ithelp.ithome.com.tw/upload/images/20240916/20168166pbHjElXz3r.png


下一篇
button / debounce
系列文
Angular 元件煉成練習計畫12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言